package helexis.game.tyrian.screen;

import helexis.game.tyrian.Assets;
import helexis.game.tyrian.Tyrian;
import helexis.game.tyrian.domain.FrontGun;
import helexis.game.tyrian.domain.Item;
import helexis.game.tyrian.domain.Profile;
import helexis.game.tyrian.domain.Shield;
import helexis.game.tyrian.domain.Ship;
import helexis.game.tyrian.domain.ShipModel;
import helexis.game.tyrian.service.SoundManager.TyrianSound;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class StartGameScreen extends AbstractScreen {
	
	/**
	 * 레벨 (에피소드) 선택 시 수행할 리스너
	 */
	private LevelListener levelListener;
	/**
	 * 아이템 선택 시 수행할 리스너
	 */
	private ItemListener itemListener;
	
	/**
	 * 레벨 1 (에피소드 1) 버튼
	 */
	private TextButton level1Button;
	/**
	 * 레벨 2 (에피소드 2) 버튼
	 */
	private TextButton level2Button;
	/**
	 * 레벨 3 (에피소드 3) 버튼
	 */
	private TextButton level3Button;
	
	/**
	 * 캐릭터 기종 선택 선택 박스
	 */
	private SelectBox shipModelSelectBox;
	/**
	 * 캐릭터 기종 프리뷰 이미지
	 */
	private Image shipModelImage;
	
	/**
	 * 전방 총 선택 박스
	 */
	private SelectBox frontGunSelectBox;
	/**
	 * 전방 총 프리뷰 이미지
	 */
	private Image frontGunImage;
	
	/**
	 * 쉴드 선택 박스
	 */
	private SelectBox shieldSelectBox;
	/**
	 * 쉴드 프리뷰 이미지
	 */
	private Image shieldImage;
	
	/**
	 * 돈 레이블
	 */
	private Label creditsLabel;
	
	
	/**
	 * 저장된 사용자 Profile 정보
	 */
	private Profile profile;
	/**
	 * 캐릭터
	 */
	private Ship ship;
	
	
	public StartGameScreen(Tyrian game) {
		super(game);
		
		this.levelListener = new LevelListener();
		this.itemListener = new ItemListener();
		
		this.profile = game.getProfileManager().retrieveProfile();
		this.ship = profile.getShip();
		
	}//end of constructor
	
	
	
	@Override
	public void show() {

		super.show();
		
		if (table == null) {
			this.init();
		}//end if
		
		this.updateValues();
		
	}//end of show()
	
	
	
	private void init() {
		
		Skin skin = getSkin();
		
		table = new Table(skin);
		table.setFillParent(true);
		stage.addActor(table);
		
		table.add("Start Game").colspan(5).spaceBottom(20);
		table.row();
		
		table.defaults().pad(8).spaceBottom(10);
		
		/*
		 * 레벨 설정
		 */
		table.add("Levels").center();
		
		this.level1Button = new TextButton("Episode 1", skin);
		this.level1Button.addListener(this.levelListener);
		table.add(this.level1Button).fillX();
		
		this.level2Button = new TextButton("Episode 2", skin);
		this.level2Button.addListener(this.levelListener);
		table.add(this.level2Button).fillX();
		
		this.level3Button = new TextButton("Episode 3", skin);
		this.level3Button.addListener(this.levelListener);
		table.add(this.level3Button).fillX();
		
		table.row();
		
		
		/*
		 * Ship Model 설정
		 */
		table.add("Ship Model").center();
		
		this.shipModelSelectBox = new SelectBox(ShipModel.values(), skin);
		this.shipModelSelectBox.addListener(this.itemListener);
		table.add(this.shipModelSelectBox).colspan(3).fillX();
		
		this.shipModelImage = new Image();
		table.add(this.shipModelImage);
		
		table.row();
		
		
		
		/*
		 * Front Gun 설정
		 */
		table.add("Front Gun").center();
		
		this.frontGunSelectBox = new SelectBox(FrontGun.values(), skin);
		this.frontGunSelectBox.addListener(this.itemListener);
		table.add(this.frontGunSelectBox).colspan(3).fillX();
		
		this.frontGunImage = new Image();
		table.add(this.frontGunImage);
		
		table.row();
		
		
		/*
		 * Shield 설정
		 */
		table.add("Shield").center();
		
		this.shieldSelectBox = new SelectBox(Shield.values(), skin);
		this.shieldSelectBox.addListener(this.itemListener);
		table.add(this.shieldSelectBox).colspan(3).fillX();
		
		this.shieldImage = new Image();
		table.add(this.shieldImage);
		
		table.row();
		
		
		/*
		 * Credits 설정
		 */
		table.add("Credits").center();
		this.creditsLabel = new Label("", skin);
		table.add(this.creditsLabel).colspan(4).left();
		
		table.row();
		
		
		/*
		 * Back Button 설정
		 */
		TextButton backButton = new TextButton("Back to main menu", skin);
		backButton.addListener(new ClickListener() {
			
			@Override
			public void clicked(InputEvent event, float x, float y) {
				super.clicked(event, x, y);
				game.getSoundManager().play(TyrianSound.CLICK);
				game.setScreen(game.getMenuScreen());
			}//end of clicked()
			
		});
		table.add(backButton).size(300, 60).colspan(5).center();
		
		/*
		 * 프리뷰 이미지 초기화
		 */
		Assets.initPreviewImages(atlas);
		
	}//end of init()
	
	
	private TextureRegionDrawable selectShipModelImage(ShipModel shipModel) {
		
		if (shipModel == ShipModel.GENCORE_PHOENIX) {
			return Assets.preview_shipModel_GENCORE_PHOENIX;
		} else if (shipModel == ShipModel.GENCORE_II) {
			return Assets.preview_shipModel_GENCORE_II;
		} else if (shipModel == ShipModel.MICROSOL_STALKER) {
			return Assets.preview_shipModel_MICROSOL_STALKER;
		} else if (shipModel == ShipModel.SUPER_CARROT) {
			return Assets.preview_shipModel_SUPER_CARROT;
		}//end if else
		
		return Assets.preview_shipModel_USP_TALON;
		
	}//end of selectShipModelImage()
	
	
	private TextureRegionDrawable selectFrontGunImage(FrontGun frontGun) {
		
		if (frontGun == FrontGun.MISSILE_LAUNCHER) {
			return Assets.preview_frontGun_MISSILE_LAUNCHER;
		} else if (frontGun == FrontGun.VULCAN_CANNON) {
			return Assets.preview_frontGun_VULCAN_CANNON;
		} else if (frontGun == FrontGun.PROTON_LAUNCHER) {
			return Assets.preview_frontGun_PROTON_LAUNCHER;
		} else if (frontGun == FrontGun.WAVE_CANNON) {
			return Assets.preview_frontGun_WAVE_CANNON;
		}//end if else
		
		return Assets.preview_frontGun_PULSE_CANNON;
		
	}//end of selectFrontGunImage()


	private TextureRegionDrawable selectShieldImage(Shield shield) {
		
		if (shield == Shield.AIF) {
			return Assets.preview_shield_AIF;
		} else if (shield == Shield.GLES) {
			return Assets.preview_shield_GLES;
		} else if (shield == Shield.GHEF) {
			return Assets.preview_shield_GHEF;
		} else if (shield == Shield.MLXS) {
			return Assets.preview_shield_MLXS;
		}//end if else
		
		return Assets.preview_shield_SIF;
		
	}//end of selectShieldImage()
	
	
	private void updateValues() {
		
		this.shipModelSelectBox.setSelection(ship.getShipModel().ordinal());
		this.frontGunSelectBox.setSelection(ship.getFrontGun().ordinal());
		this.shieldSelectBox.setSelection(ship.getShield().ordinal());
		
		this.shipModelImage.setDrawable(this.selectShipModelImage(ship.getShipModel()));
		this.frontGunImage.setDrawable(this.selectFrontGunImage(ship.getFrontGun()));
		this.shieldImage.setDrawable(this.selectShieldImage(ship.getShield()));
		
		
		this.creditsLabel.setText(profile.getCreditsAsText());
		
	}//end of updateValues()
	
	
	private class LevelListener extends ClickListener {
		
		@Override
		public void clicked(InputEvent event, float x, float y) {
			
			super.clicked(event, x, y);
			
			int levelId;
			Actor actor = event.getListenerActor();
			
			if (actor == level1Button) {
				levelId = 0;
			} else if (actor == level2Button) {
				levelId = 1;
			} else if (actor == level3Button) {
				levelId = 2;
			} else {
				return;
			}//end if else
			
			game.getSoundManager().play(TyrianSound.CLICK);
			
			if (profile.getCurrentLevelId() >= levelId) {
				Gdx.app.log(Tyrian.LOG, "Starting level [" + profile.getCurrentLevelId() + "] : " + levelId);
				game.setScreen(new LevelScreen(game, levelId));
			} else {
				Gdx.app.log(Tyrian.LOG, "Unable to start level [" + profile.getCurrentLevelId() + "] : " + levelId);
			}//end if else
			
		}//end of clicked()
		
	}//end of inner-class LevelListener
	
	
	private class ItemListener extends ChangeListener {
		
		@Override
		public void changed(ChangeEvent event, Actor actor) {
			
			Item selectedItem = null;
			
			int selectedIndex = ((SelectBox)actor).getSelectionIndex();
			
			if (actor == shipModelSelectBox) {
				selectedItem = ShipModel.values()[selectedIndex];
			} else if (actor == frontGunSelectBox) {
				selectedItem = FrontGun.values()[selectedIndex];
			} else if (actor == shieldSelectBox) {
				selectedItem = Shield.values()[selectedIndex];
			} else {
				return;
			}//end if else
			
			/*
			 * 이미 구매한 아이템을 가지고 있으면,
			 * 아무것도 안 함
			 */
			if (ship.contains(selectedItem)) {
				return;
			}//end if
			
			/*
			 * 아이템을 구매하고 돈을 빼 줌...!
			 */
			if (profile.buy(selectedItem)) {
				// 아이템 사면 뭘 해야 할까나??
			}//end if
			
			updateValues();
			
			game.getSoundManager().play(TyrianSound.CLICK);
			
		}//end of changed()
		
	}//end of inner-class ItemListener
	
	
}
//end of StartGameScreen.java